<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- STYLESHEETS -->
    <!--[if lt IE 9]>
    <script src="../../js/flot/excanvas.min.js"></script>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
    <link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css">
    <link rel="stylesheet" type="text/css" href="../../css/themes/default.css" id="skin-switcher">
    <link rel="stylesheet" type="text/css" href="../../css/responsive.css">

    <link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- file-input -->
    <link rel="stylesheet" type="text/css" href="../../js/file-input/fileinput.css"/>
    <!-- SELECT2 -->
    <link rel="stylesheet" type="text/css" href="../../js/select2/select2.min.css"/>
    <!-- UNIFORM -->
    <link rel="stylesheet" type="text/css" href="../../js/uniform/css/uniform.default.min.css"/>
    <!-- datatable -->
    <link rel="stylesheet" href="../../js/datatables/datatable.css">
    <!-- WIZARD -->
    <link rel="stylesheet" type="text/css" href="../../js/bootstrap-wizard/wizard.css"/>
    <link rel="stylesheet" href="../../js/datepicker/datepicker.css">
    <!-- FONTS
<link href='http://fonts.useso.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
--> <!-- file-input -->
    <link rel="stylesheet" type="text/css" href="../../js/file-input/fileinput.css"/>
    <!-- JQUERY -->
    <script src="../../js/jquery/jquery-2.0.3.min.js"></script>
    <script src="../../js/common.js"></script>

    <style>

        #main{
            min-height: 400px;
            display: block;
        }
        .left {
            float: left;
            width: 45%;
        }

        .right {
            float: left;
            width: 55%;
            position: relative;
        }

        .floatDiv {
            width: 160px;
            height: 130px;
            border: 1px dotted blue;
            padding: 10px;
            position: relative;
            left: 65%;
            top: -180px;
        }
    </style>
</head>

<body>
<header class="navbar clearfix" id="header">
</header>

<!-- PAGE -->
<section id="page">
    <!-- SIDEBAR -->
    <div id="sidebar" class="sidebar">
        <div class="sidebar-menu nav-collapse">

            <!-- SIDEBAR MENU -->
            <ul>

            </ul>
            <!-- /SIDEBAR MENU -->
        </div>

    </div>
    <!-- /SIDEBAR -->
    <div id="main-content">

        <div class="container">
            <div class="row">
                <div id="content" class="col-lg-12" style="padding-top: 20px">
                    <!-- PAGE HEADER-->
                    <form id="main">
                        <div class="left">

                            <div class="form-group mrg-bt10" style="display: none;" id="contractCodeDiv">
                                <label class="control-label col-md-3"><span class="required">*</span>合同编号：</label>
                                <div class="col-md-8 inline" >

                                </div>
                            </div>

                            <div class="form-group mrg-bt10">
                                <label class="control-label col-md-3"><span class="required">*</span>供应商名称：</label>
                                <div class="col-md-8 inline">
                                    <select id="supplier_select" name="supplierId" style="width: 50%">
                                        <option value="0">请选择供应商</option>
                                    </select>
                                    <span class="error-span"></span>
                                </div>
                            </div>
							<div class="form-group mrg-bt10 divCode" style="display:none">
                                <label class="control-label col-md-3 ">合同编号：</label>
                                <div class="col-md-8 inline">
                                    <input class="form-control width-50" name="contractCode" placeholder="根据框架生成" type="text" readonly>
                                    <span class="error-span"></span>
                                </div>
                                 
                            </div>
                            <div class="form-group mrg-bt10">
                                <label class="control-label col-md-3"><span class="required">*</span>合同金额：</label>
                                <div class="col-md-8 inline">
                                    <input class="form-control width-50" name="contractPrice" placeholder="请输入合同金额"
                                           type="text" readonly>
                                    <span class="error-span"></span>
                                </div>
                            </div>

                            <div class="form-group mrg-bt10 " style="min-height: 180px">
                                <label class="control-label col-md-3"><span class="required">*</span><span class="categoryLabel"> </span>：</label>
                                <div class="col-md-8  categoryDiv " style="width: 60%">


                                </div>
                            </div>

                            <div class="form-group mrg-bt10">
                                <label class="control-label col-md-3"><span class="required">*</span>订货量：</label>
                                <div class="col-md-8 inline">
                                    <input class="form-control width-50" name="quantity" placeholder="请输入订货量"
                                           type="text"
                                           readonly>
                                    <span class="error-span"></span>
                                </div>
                            </div>

                            <div class="form-group mrg-bt10">
                                <label class="control-label col-md-3"><span class="required">*</span>负责人：</label>
                                <div class="col-md-8 inline">
                                    <input class="form-control width-50" name="leader" placeholder="请输入负责人" type="text">
                                    <span class="error-span"></span>
                                </div>
                                 
                            </div>
 <div class="form-group mrg-bt10 div_cancelReason" style="display:none" >
 <label class="control-label col-md-3 " >作废原因：</label>
                                 <div class="col-md-3 inline  cancelReason">
                                    
                                    
                                </div>
 </div>
                            <div class="form-group mrg-bt10 earnestBox display-none ">
                                <label class="control-label col-md-3"><span class="required">*</span>是否保证金：</label>
                                <div class="col-md-8 inline" style="display: flex;">

                                         <input type="radio" name="isEarnestMoney" value="0" style="margin-top: 10px" /> 否
                                         <input type="radio" name="isEarnestMoney" value="1" style="margin-top: 10px"/> 是
                                         <input class="form-control width-50 display-none" name="earnestMoney" placeholder="请输入保证金" type="text">

                                </div>
                            </div>

                            <div class="form-group mrg-bt10 supplementCountBox display-none">
                                <label class="control-label col-md-3"><span class="required">*</span>附加协议：</label>
                                <div class="col-md-8 inline">
                                    <span name="supplementCount"></span>
                                </div>
                            </div>


                        </div>

                        <div class="right">
							<div class="form-group mrg-bt10">
                                <label class="control-label col-md-3"><span class="required">*</span>框架协议号：</label>
                                <div class="col-md-8 inline">
                                    <select name="frameCode" style="width: 50%">
                                        <option value="0">请选择框架</option>
                                    </select>
                                    <span class="error-span"></span>
                                </div>
                            </div>
							
                            <div class="form-group mrg-bt10">
                                <label class="control-label col-md-3"><span class="required">*</span>合同类型：</label>
                                <div class="col-md-8 inline">
                                    <select name="contractMode" style="width: 50%">
                                        <option value="Direct">经销</option>
                                        <option value="Agent">代销</option>
                                    </select>
                                    <span class="error-span"></span>
                                </div>
                            </div>



                            <div class="form-group mrg-bt10">
                                <label class="control-label col-md-3"><span class="required">*</span>结算方式：</label>
                                <div class="col-md-8 inline">
                                    <select name="settlementMode" style="width: 50%">

                                    </select>
                                    <span class="error-span"></span>
                                </div>
                            </div>

                            <div class="form-group mrg-bt10 " id="rebateBox" style="display: none">
                                <label class="control-label col-md-3"><span class="required">*</span>代销扣点：</label>
                                <div class="col-md-8 inline">
                                    <input type="text" name="discount" class="form-control width-50"
                                    /> %
                                    <span class="error-span"></span>
                                </div>
                            </div>

                            <div class="form-group mrg-bt10">
                                <label class="control-label col-md-3"><span class="required">*</span>结算类型：</label>
                                <div class="col-md-8 inline">
                                    <select name="settlementType" style="width: 50%">
                                        <option value="Mg">款到发货</option>
                                        <option value="Gm">货到付款</option>
                                    </select>
                                    <span class="error-span"></span>
                                </div>
                            </div>

                            <div class="form-group mrg-bt10 percentBox" style="min-height: 120px" id="billBox">
                                <label class="control-label col-md-3">结算金额/比例：</label>
                                <div class="col-md-8  percentDiv ">

                                    <div>
                                        预付款 ￥ <input type="text" name="preMoney" class="form-control mo"
                                                     style="width: 30%;display: inline" value="0"  /> <input type="text"
                                                                                                           name="prePercent"
                                                                                                           class="form-control "
                                                                                                           style="width: 12%;display: inline"
                                                                                                           value="0"

                                    />%
                                    </div>

                                    <div>
                                        提货款 ￥ <input type="text" name="pickMoney" class="form-control mo"
                                                     style="width: 30%;display: inline" value="0"/> <input type="text"
                                                                            earnestBox                               name="pickPercent"
                                                                                                           class="form-control "
                                                                                                           style="width: 12%;display: inline"
                                                                                                           value="0" />%
                                    </div>

                                    <div>
                                        尾　款 ￥ <input type="text" name="endMoney" class="form-control mo"
                                                     style="width: 30%;display: inline" value="0" /> <input type="text"
                                                                                                           name="endPercent"
                                                                                                           class="form-control "
                                                                                                           style="width: 12%;display: inline"
                                                                                                           value="0" />%
                                    </div>

                                </div>
                            </div>

                            <div class="form-group mrg-bt10">
                                <label class="control-label col-md-3">备注：</label>
                                <div class="col-md-8 inline">
                                    <textarea name="remark" class="width-50" style="width:90%;" rows=3></textarea>
                                </div>
                            </div>
                            </div>



                    </form>

                    <div class="detail_list " style="clear: both">



                        <a href="javaScript:;" class="btn btn-success saveBtn " style="float: right">
                            <i class="fa  fa-plus-square-o"></i> 保存
                        </a>

                        <a href="javaScript:;" class="btn btn-success downloadBtn">
                            模板下载
                        </a>

                        <div style="display: inline; margin-left:5px; margin-right: 10px;" class="addnewimg" id="f_span">
													<span class="btn btn-success btn-file" id="fir">
														<span id="two">导入明细</span>
															<input name="uploadFile" upload-file="imageUrlProduct"
                                                                   type="file">
													</span>

                        </div>

                        <div class="box border primary" style="margin-top: 15px">
                            <div class="box-title">
                                明细列表
                            </div>
                            <div class="box-body" style="min-height: 200px">
                                <table id="importData" cellpadding="0" cellspacing="0" border="0"
                                       class="datatable table table-striped table-bordered table-hover">

                                </table>
                            </div>
                        </div>


                    </div>
                </div>
                <!-- /CONTENT-->
            </div>
        </div>
    </div>
</section>
<!-- JQUERY -->
<script src="../../js/jquery/jquery-2.0.3.min.js"></script>
<script src="../../js/common.js"></script>
<!-- JQUERY UI-->
<script src="../../js/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js"></script>
<!-- BOOTSTRAP -->
<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>
<!-- SLIMSCROLL -->
<script type="text/javascript" src="../../js/jQuery-slimScroll-1.3.0/jquery.slimscroll.min.js"></script>
<script type="text/javascript" src="../../js/jQuery-slimScroll-1.3.0/slimScrollHorizontal.min.js"></script>
<!-- BLOCK UI -->
<script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>
<!--bootbox-->
<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
<!-- AUTOSIZE -->
<script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>
<!-- spinner -->
<script src="../../js/spinner/spin.js"></script>
<!-- bootstrap-fileupload -->
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.iframe-transport.js"></script>
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload.min.js"></script>
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload-process.min.js"></script>
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload-validate.min.js"></script>

<!-- INPUT MASK -->
<script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>

<!-- SELECT2 -->
<script type="text/javascript" src="../../js/select2/select2.min.js"></script>
<!-- UNIFORM -->
<script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>
<!-- WIZARD -->
<script src="../../js/bootstrap-wizard/jquery.bootstrap.wizard.min.js"></script>
<!-- WIZARD -->
<script src="../../js/jquery-validate/jquery.validate.min.js"></script>
<script src="../../js/jquery-validate/additional-methods.min.js"></script>
<script src="../../js/bootstrap-wizard/form-wizard.js"></script>
<!-- NESTABLE LISTS -->
<script type="text/javascript" src="../../js/nestable/jquery.nestable.min.js"></script>
<!-- COOKIE -->
<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
<!-- CUSTOM SCRIPT -->
<script src="../../js/script.js"></script>

<script src="../../js/Sortable.min.js"></script>

<!-- DATA TABLES -->
<script type="text/javascript" src="../../js/datatables/bootstrap-select.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable-bootstrap.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable-tabletools.js"></script>
<script type="text/javascript" src="../../js/datatables/fnAddTr.js"></script>
<script>
    var datetables = null;
    var id = $.getUrlParam("id");
    var opType = $.getUrlParam("opType");
    var contractType =  $.getUrlParam("contractType");
    var supplementCount = $.getUrlParam("supplementCount");
    var init = function () {
        App.init();
        initContractType();
        if(contractType == "Material") {
            initMaterialForm();
        }else{
            initGoodsForm();
        }


        initSupplier();
        calculateBillPercent();

        $('select').select2();

        //新增合同
        if (id == null) {
            initDirectSelect();
        }


        //初始化编辑
        initEditData();



    }

    var initContractType = function() {
        if(id == null) {
            return ;
        }
        $.ajax({
            type: "get",
            url: App.getContextPath() + "admin/contract/findById.do?id="+id,
            async:false,
            success: function (data) {
                contractType = data.contractType;
            }
        });
    }

    /**初始化大伙
     * **/
    var initGoodsForm = function() {
        $(".categoryLabel").html("产品分类");
        initCategory();
    }

    /**
     * 初始化物料
     **/
    var initMaterialForm = function() {
        $(".categoryLabel").html("物料分类");
        $(".earnestBox").show();
        $("[name=isEarnestMoney]").eq(0).attr("checked","checked");
        initMaterialCategory();
    }


    //导入模板
    $("[upload-file=imageUrlProduct]").fileupload({
        url: App.getContextPath() + "admin/contract/loadDetail.do?contractType="+contractType,
        dataType: 'json',
        acceptFileTypes: /(\.|\/)(xlsx)$/i,
        iframe: true,
        start: function (e) {
            $('.progress').fadeIn(300);
            return true;
        },
        autoUpload: true,
        done: function (e, data) {
            if (data.result.type == 'error') {
                $.alert("导入模板错误");
            } else {

                initDateTable(data.result.extra);
            }

        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('.progress .progress-bar').css('width', progress + '%');
        }
    }).on('fileuploaddone', function (e, data) {
        $(".progress").fadeOut(300);

    });

    //下载
    $(".downloadBtn").click(function () {
        if(contractType == "Material") {
            location.href = App.getContextPath() + "admin/contract/downloadMaterialTemplate.do";
        }else{
            location.href = App.getContextPath() + "admin/contract/downloadGoodsContractTemplate.do";
        }
    });

    $("[name=contractMode]").change(function () {

        if ($(this).val() == "Direct") {
            initDirectSelect();
        } else if ($(this).val() == "Agent") {
            initAgentSelect();
        } else {
            //
        }
        $("[name=settlementMode]").trigger("change");
    });

    $("[name=settlementMode]").change(function () {
        if ($(this).val() == "Bill") {
            triggerBill();
        } else if ($(this).val() == "Rebate") {
            triggerRebate();
        } else {
            //
        }
    });

    var checkForm = function (mainJson) {

        if (datetables == null) {
            $.alert("请先导入明细");
            return false;
        }

        if ($("[name=supplierId] option:selected").val() == 0) {
            $.alert("请选择供应商");
            return false;
        }

        if (mainJson.categoryId == null) {
            $.alert("请选择产品分类");
            return false;
        }

        if ($("[name=leader]").val() == "") {
            $.alert("请填写负责人");
            return false;
        }
        if ($("[name=settlementMode] option:selected").val() == 'Rebate' && $("[name=discount]").val() == "") {
            $.alert("请填写扣点");
            return false;
        }
        if($("[name=settlementMode] option:selected").val() == 'Bill' && contractType != "Material"){

            if(!$("[name=earnestMoney]").is(":hidden") && $("[name=earnestMoney]").val() == "") {
                $.alert("请填写保证金");
                return false;
            }

            if ($("[name=preMoney]").val() == "") {
                $.alert("请填写预付款");
                return false;
            }

            if ($("[name=prePercent]").val() == "") {
                $.alert("请填写预付款比例");
                return false;
            }

            if ($("[name=pickMoney]").val() == "") {
                $.alert("请填写提货款");
                return false;
            }

            if ($("[name=pickPercent]").val() == "") {
                $.alert("请填写提货款比例");
                return false;
            }

            if ($("[name=endMoney]").val() == "") {
                $.alert("请填写尾款");
                return false;
            }

            if ($("[name=endPercent]").val() == "") {
                $.alert("请填写尾款比例");
                return false;
            }

            var ep = $("[name=endPercent]").val();
            var ip = $("[name=pickPercent]").val();
            var pp = $("[name=prePercent]").val();
            var amount  = Number(ip)+Number(ep)+Number(pp);
            if ( !(amount > 99 && amount<=100 ) ) {
                $.alert("结算比例需等于100%");
                return false;
            }


        }


        return true;
    }
    $(".saveBtn").click(function () {

        var mainJson = $("#main").serializeObject();

        if (!checkForm(mainJson)) {
            return false;
        }

        var api = datetables.api();
        var datas = api.rows().data();
        var amount = 0;
        var goodsAccount = 0;
        var contractDetails = new Array();
        for (var i = 0; i < datas.length; i++) {
            if (datas[i].remark != "" ) {
                $.alert("订单数据有误");
                return;
            }
            contractDetails.push(datas[i]);
        }


        mainJson.contractDetails = contractDetails;
        mainJson.contractType = contractType;
        var temp = "";
        if (mainJson.categoryId  instanceof Array) {

            mainJson.categoryId.forEach(function (e) {
                temp += e + ","
            })
            temp = temp.substr(0, temp.length - 1);
        } else {
            temp = mainJson.categoryId;

        }
        mainJson.categoryId = temp;
        mainJson.id = id;
        if(mainJson.frameCode!="0"){
        	$.confirm("确定以框架形式提交合同?",function(){
        		submitContract(contractType,mainJson);
        	});
        }else{
        	mainJson.frameCode="";
        	submitContract(contractType,mainJson);
        	
        }
        


    });
var submitContract=function(contractType,mainJson){
	var json= JSON.stringify(mainJson)	
     	$.ajax({
             type: "post",
             url: App.getContextPath() + "admin/contract/saveContract.do",
             data: {contractStr: json},
             async: false,
             success: function (result) {
                 if (result.type == "error") {
                     $.alert(result.content);
                 } else {
                     location.href = "contract.html?contractType="+contractType;
                 }
             }
         });
}

    /**
     * 初始化经销select
     */
    var initDirectSelect = function () {
        var html ='<option value="Bill">按进货单结算</option>';
        $("[name=settlementMode]").html(html);
        $("[name=settlementMode]").select2();
    }

    /**
     * 初始化代销select
     * */
    var initAgentSelect = function () {
        var html ='<option value="Bill">按进货单结算</option>';
        html +='<option value="Rebate">按扣点结算</option>';
        $("[name=settlementMode]").html(html);
        $("[name=settlementMode]").select2();
    }
    /**
     * 触发按进货单结算
     */
    var triggerBill = function () {
        $("#billBox").show();
        $("#rebateBox").hide();
    }

    /**
     * 触发按扣点结算
     */
    var triggerRebate = function () {
        $("#rebateBox").show();
        $("#billBox").hide();
    }

    $("[name=isEarnestMoney]").click(function () {
        if($(this).val() == 1){
            $("[name=earnestMoney]").show();
        }else {
            $("[name=earnestMoney]").hide();
        }
    });

    /**
     * 计算结算金额比例
     */
    var calculateBillPercent = function () {

        $("[name=preMoney]").keyup(function () {

            var contractPrice = $("[name=contractPrice]");
            var preMoney = $("[name=preMoney]");
            var prePercent = $("[name=prePercent]");

            if (contractPrice.val() == "" || contractPrice.val() <= 0  )
                return;

            prePercent.val(($(this).val() / contractPrice.val()) * 100);


        });

        $("[name=prePercent]").keyup(function () {
            var contractPrice = $("[name=contractPrice]");
            var preMoney = $("[name=preMoney]");
            var prePercent = $("[name=prePercent]");

            if (contractPrice.val() == "" || contractPrice.val() <= 0)
                return;

            preMoney.val( ((contractPrice.val()*$(this).val())/100).toFixed(2) );

        });




        $("[name=pickMoney]").keyup(function () {
            var contractPrice = $("[name=contractPrice]");
            var pickPercent = $("[name=pickPercent]");

            if (contractPrice.val() == "" || contractPrice.val() <= 0 )
                return;

            pickPercent.val(($(this).val() / contractPrice.val()) * 100  );

        });


        $("[name=pickPercent]").keyup(function () {
            var contractPrice = $("[name=contractPrice]");
            var pickMoney = $("[name=pickMoney]");
            var pickPercent = $("[name=pickPercent]");

            if (contractPrice.val() == "" || contractPrice.val() <= 0)
                return;

            pickMoney.val( ((contractPrice.val()*$(this).val())/100).toFixed(2) );

        });



        $("[name=endMoney]").keyup(function () {
            var contractPrice = $("[name=contractPrice]");
            var endPercent = $("[name=endPercent]");

            if (contractPrice.val() == "" || contractPrice.val() <= 0 )
                return;

            endPercent.val(($(this).val() / contractPrice.val()) * 100);

        });


        $("[name=endPercent]").keyup(function () {
            var contractPrice = $("[name=contractPrice]");
            var endMoney = $("[name=endMoney]");

            if (contractPrice.val() == "" || contractPrice.val() <= 0)
                return;

            endMoney.val( ((contractPrice.val()*$(this).val())/100).toFixed(2) );

        });

    }



    var initDateTable = function (data) {
        var dtSetting = {
            "serverSide": false,
            "bFilter": false,// 去掉搜索框
            "sScrollX": "100%",
            "sScrollXInner": "100%",
            "bSort": false,
            "paging": false,
            "data": data,
            "info": false,
            "bDestroy": true,
            "drawCallback": function (settings) {
                var api = this.api();
                var datas = api.rows().data();
                var amount = 0;
                var goodsAccount = 0;
                for (var i = 0; i < datas.length; i++) {
                    var e = datas[i];
                    amount += e.price * e.quantity;
                    goodsAccount += e.quantity;
                }
                $("[name=contractPrice]").val(parseFloat(amount).toFixed(2));
                $("[name=quantity]").val(goodsAccount);

                $("[name=preMoney]").val( ((amount*$("[name=prePercent]").val())/100).toFixed(2) );
                $("[name=pickMoney]").val( ((amount*$("[name=pickPercent]").val())/100).toFixed(2) );
                $("[name=endMoney]").val( ((amount*$("[name=endPercent]").val())/100).toFixed(2) );

//                var isNewList = null;
//                var tempCount = 0;
//                for(var i=0;i<datas.length;i++){
//                    if(datas[i].newProduct == null) {
//                        tempCount++;
//                    }
//                }
//                if(tempCount == datas.length) {
//                    api.column(8).visible( false );
//                }

            },
            "aoColumns": [{
                "mDataProp": "skuCode",
                "sTitle": "SKU编码",
                "width": "6%"
            }, {
                "mDataProp": "outCode",
                "sTitle": "款号",
                "width": "6%"
            },{
                "mDataProp": "productName",
                "sTitle": "商品名称",
                "width": "10%"
            }, {
                "mDataProp": "color",
                "sTitle": "颜色",
                "width": "6%"
            }, {
                "mDataProp": "sizeName",
                "sTitle": "尺码大小",
                "width": "6%"
            }, {
                "mDataProp": "quantity",
                "sTitle": "数量",
                "width": "6%"
            }, {
                "mDataProp": "price",
                "sTitle": "单价",
                "width": "6%"
            }, {
                "sTitle": "SKU金额",
                "width": "6%",
                "render": function (data, type, row) {
                    return (row.quantity * row.price).toFixed(4);
                }
            }, {
                "mDataProp": "arrivedDate",
                "sTitle": "预计到货日期",
                "width": "6%",
                "render": function (data, type, row) {
                    if(data == null){
                        return "";
                    }
                    return $.getLocalDate(data);
                }
            }, {
                "mDataProp": "isNewProduct",
                "sTitle": "是否新品",
                "width": "6%",
                "render": function (data, type, row) {

                    if (row.newProduct == true) {
                        return "是";
                    } else if (row.newProduct == false){
                        return "否";
                    }else {
                        return "";
                    }

                }
            }, {
                "mDataProp": "remark",
                "sTitle": "错误提示",
                "width": "10%",
                "render": function (data, type, row) {
                    if (row.remark == null) {
                        return "";
                    } else {
                        return "<span style='color: red;'>" + row.remark + "</span>";
                    }
                }
            }]
        };
        datetables = $("#importData").dataTable(dtSetting);

    }

    var initEditData = function () {
        if (id == null) {
            return;
        }

        $.ajax({
            type: "get",
            url: App.getContextPath() + "admin/contract/findDetailById.do?id=" + id,
            async: false,
            success: function (data) {

                //只读
                if(opType == "view") {
                	$(".divCode").hide();
                    $("input").attr("disabled","disabled");
                    $("select").attr("disabled","disabled").select2();
                    $("textarea").attr("disabled","disabled");
                    $(".btn").remove();
                   // $(".supplementCountBox").show();
                    $("[name=supplementCount]").html(data.supplementCount);
                    if(data.contractCode == null) {
                        data.contractCode = "合同归档后生成";
                    }
                    $("#contractCodeDiv div").html(data.contractCode);
                    $("#contractCodeDiv").show();
                    if(data.contractStatus=="Cancel"){
                    	$(".div_cancelReason").show();
                    	$(".cancelReason").html(data.cancelReason);
                    }

                }

                if(data.contractMode == "Direct"){
                    initDirectSelect();
                }else {
                    initAgentSelect();
                }

                $("[name=supplierId]").select2("val", data.supplierId);
                $("[name=contractPrice]").val(data.contractPrice);
                if(data.frameCode){
                	$("[name=supplierId]").trigger("change");
                	 $("[name=frameCode]").select2("val",data.frameCode);
                }
               

                var cids = data.categoryId.split(",");
                cids.forEach(function (cid) {

                    $("[name=categoryId]").each(function () {
                        if ($(this).val() == cid) {
                            $(this).attr("checked", "checked");
                        }
                    })

                })

                $("[name=quantity]").val(data.quantity);
                $("[name=leader]").val(data.leader);
				$("[name=contractCode]").val(data.contractCode);
                $("[name=contractMode]").select2("val", data.contractMode);
                $("[name=settlementMode]").select2("val", data.settlementMode);
                $("[name=settlementType]").select2("val", data.settlementType);
                $("[name=preMoney]").val(data.preMoney.toFixed(2));
                $("[name=prePercent]").val(data.prePercent*100);
                $("[name=pickMoney]").val(data.pickMoney.toFixed(2));
                $("[name=pickPercent]").val(data.pickPercent*100);
                $("[name=endMoney]").val(data.endMoney.toFixed(2));
                $("[name=endPercent]").val(data.endPercent*100);
                $("[name=remark]").text(data.remark);
                $("[name=discount]").val(data.discount);
                $("[name=earnestMoney]").val(data.earnestMoney);
                if( data.settlementMode == "Rebate")
                {
                    triggerRebate();
                }else {
                    triggerBill();
                }

                if(  $("[name=earnestMoney]").val() == ""){
                    $("[name=isEarnestMoney]").eq(0).attr("checked","checked");
                }else {
                    $("[name=isEarnestMoney]").eq(1).attr("checked","checked");
                    $("[name=earnestMoney]").show();
                }

                initDateTable(data.contractDetails);

            }
        });

    }


    var initCategory = function () {
        $.ajax({
            type: "get",
            url: App.getContextPath() + "admin/contract/findCategory.do",
            async:false,
            success: function (data) {
                for (var i = 0,j=1; i < data.length; i++,j++) {
                    var html = "<input type='checkbox' name='categoryId' style='margin-left:1px' value='" + data[i ].id + "'/> " + data[i].categoryName;
                    if (j % 4 == 0) {
                        html += "<br>";
                    }
                    $(".categoryDiv").append(html);
                }

            }
        });
    }

    var initMaterialCategory= function () {

        $.ajax({
            type: "get",
            url: App.getContextPath() + "admin/material/searchMaterialList.do",
            async:false,
            success: function (data) {
                for (var i = 0,j=1; i < data.length; i++,j++) {
                    var html = "<input type='checkbox' name='categoryId' style='margin-left:1px' value='" + data[i ].id + "'/> " + data[i].name;
                    if (j % 3 == 0) {
                        html += "<br>";
                    }
                    $(".categoryDiv").append(html);
                }

            }
        });
    }

    var initSupplier = function () {
        $.ajax({
            type: "get",
            async: false,
            url: App.getContextPath() + "admin/supplier/searchSupplierList.do",
            success: function (data) {
                data.forEach(function (e) {
                    $("[name=supplierId]").append("<option value='" + e.id + "'>" + e.supplierName + "</option>");
                });

            }
        });
        $("[name=supplierId]").change(getFrame);
        
        
    }
    var getFrame=function(){
    	$("[name=frameCode] option").remove();
    	$.ajax({
            type: "get",
            async: false,
            url: App.getContextPath() + "admin/frame/getFrameCode.do",
            data:{supplierId:$("[name=supplierId]").val()},
            success: function (data) {
            	$("[name=frameCode]").append("<option value=0>请选择框架</option>")
            	data.forEach(function (e) {
                    $("[name=frameCode]").append("<option value='" + e.frameCode + "'>" + e.frameCode + "</option>");
                });
            	
            	$("[name=frameCode]").select2();
            	$("[name=frameCode]").change(getContractCode);
            }
        });
    }
    var getContractCode=function(){
    	var frameCode=$("[name=frameCode]").val();
    	if($("[name=frameCode]").val()=="0"){
    		$("[name=contractCode]").val("");
    		return ;
    	}
    	$.ajax({
            type: "get",
            async: false,
            url: App.getContextPath() + "admin/contract/getContractCode.do",
            data:{frameCode:frameCode},
            success: function (data) {
            	
            	
            	var s="";
            	if(data==""){
            		s=frameCode+"-01";
            	}else{
            		s=data.replace(frameCode,'');
            		s=s.substr(1,2);
            		s=parseInt(s)+1;
            		if(s<10)s="0"+s;
            		s=frameCode+"-"+s;
            	}
            	//$("[name=contractCode]").val(s);
            	
            }
        });
    }

    $(function () {
        $("#header").load("../head.html");
        init();
    });
</script>

<!-- /JAVASCRIPTS -->
</body>

</html>